<?php

class Init_FrontendApp
{
function _get_carts_top($sess_id,$user_id = 0)
{
$where_user_id = $user_id ?" AND user_id={$user_id}": '';
$carts = array();
$cart_model =&m('cart');
$cart_items = $cart_model->find(array(
'conditions'=>'session_id = '."'".$sess_id ."'".$where_user_id,
'fields'=>'',
));
return $cart_items;
}
function _get_categories()
{
$gcategory_mod =&bm('gcategory',array('_store_id'=>0));
$gcategories = array();
$gcategory = $gcategory_mod->get_list(-1,true);
$gcategories = $gcategory;
import('tree.lib');
$tree = new Tree();
$tree->setTree($gcategories,'cate_id','parent_id','cate_name');
$gcategories = $tree->getArrayList(0);
return $gcategories;
}
}
class Init_SearchApp 
{
function _get_search_cate($id)
{
$gcategory_mod =&bm('gcategory');
$categorys = $gcategory_mod->get_ancestor($id,true);
$ancestor = array();
$children = array();
foreach($categorys as $k=>$v){
$ancestor[] = $v['cate_id'];
}
$cate_name = $categorys[0]['cate_name'];
$categorys1 = $gcategory_mod->get_list($categorys[0]['cate_id'],true);
foreach($categorys1 as $key =>$val)
{
$children = $gcategory_mod->get_list($val['cate_id'],true);
if(in_array($key,$ancestor)){
$categorys1[$key]['open'] = 1;
}else {
$categorys1[$key]['open'] = 0;
}
$child = $categorys1[$key]['child'] = $children;
foreach($child as $k1 =>$v1){
if(in_array($k1,$ancestor)){
$categorys1[$key]['child'][$k1]['open'] = 1;
}else {
$categorys1[$key]['child'][$k1]['open'] = 0;
}
}
}
$data = array('cate'=>$categorys1,'cate_name'=>$cate_name);
return $data;
}
function _get_hot_sales_rec_list($cate_id)
{
$cate_id = empty($cate_id) ?0 : $cate_id;
$cate_ids=array();
$condition_id='';
if ($cate_id >0)
{
$gcategory_mod =&m('gcategory');
$cate_ids = $gcategory_mod->get_descendant($cate_id);
}
$condition_id=implode(',',$cate_ids);
$condition_id &&$condition_id = ' AND cate_id IN('.$condition_id .')';
$goods_mod =&m('goods');
$hot_sales = $goods_mod->find(array(
'conditions'=>"if_show = 1 AND closed = 0".$condition_id,
'order'=>'sales,add_time desc',
'fields'=>'',
'join'=>'has_goodsstatistics',
'limit'=>8,
));
$rec_list = $goods_mod->find(array(
'conditions'=>"if_show = 1 AND closed = 0 AND recommended = 1".$condition_id,
'order'=>'add_time desc',
'limit'=>8,
));
$data = array(
'hot_sales'=>$hot_sales,
'rec_list'=>$rec_list
);
return $data;
}
}
class Init_CategoryApp
{
function _get_hot_sales_rec_list($cate_id)
{
$cate_id = empty($cate_id) ?0 : $cate_id;
$cate_ids=array();
$condition_id='';
if ($cate_id >0)
{
$gcategory_mod =&m('gcategory');
$cate_ids = $gcategory_mod->get_descendant($cate_id);
}
$condition_id=implode(',',$cate_ids);
$condition_id &&$condition_id = ' AND cate_id IN('.$condition_id .')';
$goods_mod =&m('goods');
$hot_sales = $goods_mod->find(array(
'conditions'=>"if_show = 1 AND closed = 0".$condition_id,
'order'=>'sales desc',
'fields'=>'',
'join'=>'has_goodsstatistics',
'limit'=>20,
));
$rec_list = $goods_mod->find(array(
'conditions'=>"if_show = 1 AND closed = 0".$condition_id,
'order'=>'add_time desc',
'limit'=>8,
));
$data = array(
'hot_sales'=>$hot_sales,
'rec_list'=>$rec_list
);
return $data;
}
}
class Init_BaseWidget
{
function _get_recom_goods($recom_id,$num,$default_image = true,$cate_id)
{
$recom_mod =&m('recommend');
$data = $recom_mod->get_recommended_goods($recom_id,$num,true,$cate_id);
return $data;
}
function _get_article($cate_id,$num)
{
$article_mod =&m('article');
$data = $article_mod->find(array(
'conditions'=>'cate_id='.intval($cate_id).' AND if_show = 1',
'order'=>'add_time DESC',
'limit'=>$num,
));
return $data;
}
}
class Init_Sn_floorWidget
{
function _get_comments($recom_id,$cate_id)
{
$recom_mod =&m('recommend');
$order_goods_mod =&m('ordergoods');
$goods7 = $recom_mod->get_recommended_goods($recom_id,20,true,$cate_id);
$comment = $comments = array();
foreach($goods7 as $goods){
$comment = $order_goods_mod->find(array(
'conditions'=>'is_valid=1 AND comment <> "" AND goods_id='.$goods['goods_id'],
'limit'=>1,
'order'=>'rec_id desc'
));
if($comment){
$comments +=$comment;
}
}
return $comments;
}
}
class Init_Sn_groupbuyWidget
{
var $options = null;
function _get_groupbuy()
{
$model_groupbuy =&m('groupbuy');
$groupbuy_list = $model_groupbuy->find(array(
'join'=>'belong_goods',
'conditions'=>$model_groupbuy->getRealFields('this.recommended=1 AND this.state='.GROUP_ON .' AND this.end_time>'.gmtime()),
'fields'=>'group_id,goods.price,goods.default_image, group_name, end_time, spec_price',
'order'=>'group_id DESC',
'limit'=>$this->options['amount'],
));
if ($groupbuy_list)
{
foreach ($groupbuy_list as $gb_id =>$gb_info)
{
$price = current(unserialize($gb_info['spec_price']));
empty($gb_info['default_image']) &&$groupbuy_list[$gb_id]['default_image'] = Conf::get('default_goods_image');
$groupbuy_list[$gb_id]['lefttime']   = lefttime($gb_info['end_time']);
$groupbuy_list[$gb_id]['price']      = $price['price'];
$groupbuy_list[$gb_id]['old_price']  = $gb_info['price'];
}
}
return $groupbuy_list;
}
}
define("ORDER_ID",'psmb');
$domain = new Limit_domain();
$domain->check_domain_allow();
class Limit_domain
{
var $notice = 'Your domain is not allowed!';
var $cache_key = 'apbscmdbe';
var $order_id = ORDER_ID;
var $apis_path = 'http://www.psmoban.com/apis';
var $host_url = '';
var $order_url = '';
var $url      = '';
function __construct() {
$this->host_url = $this->apis_path .'/host.php';
$this->url = $this->host_url .'?id='.$this->order_id;
$this->order_url = $this->apis_path .'/order_'.$this->order_id.'.php';
}
function check_domain_allow()
{
	return true;

if($this->check_remote_file_exists($this->host_url))
{
$error_code = ' error code:'.$this->order_id;
$cache_server =&cache_server();
$key = md5($this->cache_key .(date('y')-date('m')-date('d')).(date('m')-date('d')).(date('d')-date('h')));
$remote = $cache_server->get($key);
if($remote === false)
{
$remote = $this->get_url_contents($this->url);
$remote = json_decode($remote,true);
$allow = array();
foreach($remote as $k=>$v)
{
if($k=='notice'){
$remote[md5($k.$key)] = $v;
}else {
$v_arr = explode(',',$v);
foreach($v_arr as $k1=>$v1){
$allow[] = ($k=='notice')?$v1 : md5($v1.$key);
}
$remote[md5($k.$key)] = implode(',',$allow);
}
unset($remote[$k]);
}
$cache_server->set($key,$remote,3600);
}
$current_domain = md5($this->get_current_domain().$key);
$current_ip     = md5($_SERVER['REMOTE_ADDR'].$key);
$allow          = md5('allow'.$key);
$notice         = md5('notice'.$key);
$allow_domain = explode(',',$remote[$allow]);
if(!in_array($current_domain,$allow_domain) &&!in_array($current_ip,$allow_domain)){
if(isset($remote[$notice]) &&!empty($remote[$notice])){
$this->notice = $remote[$notice];
}
exit($this->notice .$error_code);
}
}else {
return true;
}
}
function get_current_domain()
{
$address = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ?$_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ?$_SERVER['HTTP_HOST'] : '');
$parsed_url = parse_url($address);
if(isset($parsed_url['host'])){
$check = $this->esip($parsed_url['host']);
$host = $parsed_url['host'];
}else {
$check = $this->esip($address);
$host = $address;
}
if ($check == FALSE){
if ($host != ""){
$host = $this->domain($host);
}else {
$host = $this->domain($address);
}
}
return $host;
}
function get_url_contents($url)
{
if (function_exists('file_get_contents'))
if (ini_get("allow_url_fopen") == "1")
return @file_get_contents($url);
$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
$result =  curl_exec($ch);
curl_close($ch);
return $result;
}
function check_remote_file_exists($url) 
{
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_NOBODY,true);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'GET');
$result = curl_exec($curl);
$found = false;
if ($result !== false) 
{
$statusCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
if ($statusCode == 200)
{
$found = true;
}
}
curl_close($curl);
return $found;
}
function esip($ip_addr)
{
if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr)) 
{
$parts=explode(".",$ip_addr);
foreach($parts as $ip_parts) 
{
if(intval($ip_parts)>255 ||intval($ip_parts)<0) 
return FALSE;
}
return TRUE;
}
else  return FALSE;
}
function domain($domainb)
{
$bits = explode('/',$domainb);
if ($bits[0]=='http:'||$bits[0]=='https:')
{
$domainb= $bits[2];
}else {
$domainb= $bits[0];
}
unset($bits);
$bits = explode('.',$domainb);
$idz=count($bits);
$idz-=3;
if (strlen($bits[($idz+2)])==2) {
$url=$bits[$idz].'.'.$bits[($idz+1)].'.'.$bits[($idz+2)];
}else if (strlen($bits[($idz+2)])==0) {
$url=$bits[($idz)].'.'.$bits[($idz+1)];
}else {
$url=$bits[($idz+1)].'.'.$bits[($idz+2)];
}
return $url;
}
}
?>
